/**
 * 
 */
package Consult.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import Consult.model.BookType;
import Util.DBUtils;
import Util.StringUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;

/**
 * @author zhaoyana
 *
 */
public class BookTypeAddInterFrm extends JInternalFrame {
	private JTextField bookTypeNameTxt;
	private JTextField bookTypeDescTxt;
	private DBUtils dbUtil=new DBUtils();
	private BookTypeDao bookTypeDao=new BookTypeDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookTypeAddInterFrm frame = new BookTypeAddInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookTypeAddInterFrm() {
		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setTitle("鍥句功绫诲埆娣诲姞");
		setBounds(100, 100, 648, 522);
		
		JLabel lblNewLabel = new JLabel("鍥句功绫诲埆鍚嶇О");
		lblNewLabel.setFont(new Font("瀹嬩綋", Font.PLAIN, 22));
		
		JLabel lblNewLabel_1 = new JLabel("鍥句功绫诲埆鎻忚堪");
		lblNewLabel_1.setFont(new Font("瀹嬩綋", Font.PLAIN, 21));
		
		bookTypeNameTxt = new JTextField();
		bookTypeNameTxt.setColumns(10);
		
		bookTypeDescTxt = new JTextField();
		bookTypeDescTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("娣诲姞");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeAddActionPerformed(e);
			}
		});
		btnNewButton.setFont(new Font("瀹嬩綋", Font.PLAIN, 20));
		
		JButton btnNewButton_1 = new JButton("鍙栨秷");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
					
				
			}
		});
		btnNewButton_1.setFont(new Font("瀹嬩綋", Font.PLAIN, 20));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(33)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(bookTypeDescTxt)
								.addComponent(bookTypeNameTxt, GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(156)
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)
							.addGap(139)
							.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(75, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(96)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel)
						.addComponent(bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(46)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_1)
						.addComponent(bookTypeDescTxt, GroupLayout.PREFERRED_SIZE, 218, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
					.addGap(35))
		);
		getContentPane().setLayout(groupLayout);

	}
	/**
	 * 鍥句功绫诲埆娣诲姞浜嬩欢澶勭悊
	 * @param e
	 */
	protected void bookTypeAddActionPerformed(ActionEvent evt) {
		String bookTypeName=this.bookTypeNameTxt.getText();
		String bookTypeDesc=this.bookTypeDescTxt.getText();
		if(StringUtil.isEmpty(bookTypeName)) {
			JOptionPane.showMessageDialog(null, "鍥句功绫诲埆涓嶈兘涓虹┖锛�");
			return;
		}// TODO Auto-generated method stub
		BookType bookType=new BookType(bookTypeName,bookTypeDesc);//灏佽瀹炰綋
        Connection con=null;
        try {
        	con=dbUtil.getCon();
        	int n=bookTypeDao.add(con, bookType);
        	
        	if(n==1) {
        		
        		JOptionPane.showMessageDialog(null, "鍥句功绫诲埆娣诲姞鎴愬姛");
        		resetValue();
        		
        	}else {
        		JOptionPane.showMessageDialog(null, "鍥句功绫诲埆娣诲姞澶辫触锛�");
        	}
        }catch(Exception e) {
        	e.printStackTrace();
        	JOptionPane.showMessageDialog(null, "鍥句功绫诲埆娣诲姞澶辫触锛�");
        	
        }finally {
        	try {
        		
        	
        	dbUtil.closeCon(con);
        	} catch(Exception e) {
        		e.printStackTrace();
        	}}
	}

	/**
	 * 
	 * 閲嶇疆浜嬩欢澶勭悊
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		this.resetValue();// TODO Auto-generated method stub
		
	}

	/**
	 * 
	 */
	private void resetValue() {
		this.bookTypeNameTxt.setText("");
		this.bookTypeDescTxt.setText("");// TODO Auto-generated method stub
		
	}

	
	
}
